package org.feifei.pet.service.impl;

import org.feifei.basic.exception.BusinessException;
import org.feifei.basic.service.impl.BaseServiceImpl;
import org.feifei.basic.util.*;
import org.feifei.order.domain.PetAcquisitionOrder;
import org.feifei.order.mapper.PetAcquisitionOrderMapper;
import org.feifei.org.domain.Employee;
import org.feifei.org.domain.Shop;
import org.feifei.org.mapper.EmployeeMapper;
import org.feifei.org.mapper.ShopMapper;
import org.feifei.pet.domain.Pet;
import org.feifei.pet.domain.SearchMasterMsg;
import org.feifei.pet.mapper.SearchMasterMsgMapper;
import org.feifei.pet.query.SearchMasterMsgQuery;
import org.feifei.pet.service.PetService;
import org.feifei.pet.service.SearchMasterMsgService;
import org.feifei.user.domain.LoginInfo;
import org.feifei.user.domain.User;
import org.feifei.user.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements SearchMasterMsgService {
    @Resource
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private PetService petService;

    @Resource
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public List<SearchMasterMsg> getAll() {
        return searchMasterMsgMapper.list();
    }

    @Transactional
    @Override
    public void pushlish(SearchMasterMsg msg, HttpServletRequest request) {
        if ( !StringUtils.hasText(msg.getTitle()) || !StringUtils.hasText(msg.getAddress())) {
            throw new BusinessException("店铺和地址不能输入为空");
        }
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        User user =  userMapper.getByLoginId(loginInfo.getId());
        Point point = DistanceUtil.getPoint(msg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.list());
        msg.setUser(user).setUser_id(user.getId()).setShop_id(nearestShop.getId());
        searchMasterMsgMapper.save(msg);

        //SmsUtil.sendMsg( nearestShop.getTel(),"你有新的订单，请到【" + msg.getAddress() + "】去收取宠物" + msg.getName());
        System.out.println("你有新的订单，请到【" + msg.getAddress() + "】去收取宠物" + msg.getName());


    }

    @Override
    public PageList<List<SearchMasterMsg>> pending(SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(0);
        return employeeGetListPageList(query, request);
    }

    @Override
    public PageList<List<SearchMasterMsg>> processed(SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(1);
        return employeeGetListPageList(query, request);
    }

    @Override
    public PageList<List<SearchMasterMsg>> user(SearchMasterMsgQuery query, HttpServletRequest request) {
        //用户查询所有消息：待处理 + 已处理
        //获取登录的管理员信息：平台 or 店铺
        LoginInfo logininfo = LoginContextUtil.getLoginInfo(request);
        //根据登录信息id 查询 管理员
        User user = userMapper.getByLoginId(logininfo.getId());
        query.setUser_id(user.getId());

        return super.listByPage(query);
    }

    @Override
    public void handle(Pet pet, HttpServletRequest request) {
        // 1.将寻主消息的状态设置为已处理
        searchMasterMsgMapper.updateStateById(pet.getSearch_master_msg_id());
        // 2.将宠物信息和进行保存
        petService.save(pet);
        // 2.1添加宠物信息
        // 2.2添加宠物明细
        // 2.3倒计时支付
        // 2.4支付 //TODO
        //3.1获取寻主消息对象
        SearchMasterMsg masterMsg = searchMasterMsgMapper.get(pet.getSearch_master_msg_id());
        LoginInfo logininfo = LoginContextUtil.getLoginInfo(request);
        Employee employee = employeeMapper.findByLoginID(logininfo.getId());

        PetAcquisitionOrder acquisitionOrder = pet2order(pet, masterMsg, employee.getId());
        //添加收购订单
        petAcquisitionOrderMapper.save(acquisitionOrder);

        //4.创建支付单 - 并完成支付 @TODO
    }

    private PageList<List<SearchMasterMsg>> employeeGetListPageList(SearchMasterMsgQuery query, HttpServletRequest request) {
        // 从登录信息中获取到所有的loginInfo信息
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        assert loginInfo != null;
        // 获取管理员的信息
        Employee employee = employeeMapper.findByLoginID(loginInfo.getId());
        // 判断店铺管理员还是 系统管理员
        if (employee.getShop_id() == null) {
            query.setShop_id(employee.getShop_id());
        }
        return super.listByPage(query);
    }

    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg masterMsg,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付 以支付1 取消-1
        order.setPrice(pet.getCostprice());
        order.setAddress(masterMsg.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(masterMsg.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(masterMsg.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        //lastcomfirmtime - 店铺给用户 - 不用倒计时定时器
        //paySn - 支付单唯一单号 - 支付的时候做
        return order;
    }
}
